package com.example.ecommercemarketing.handler.impl;

import com.example.ecommercemarketing.dto.MarketingElementsDTO;
import com.example.ecommercemarketing.dto.ProductDTO;
import com.example.ecommercemarketing.dto.UserDTO;
import com.example.ecommercemarketing.handler.MarketingElementsHandler;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 通过榜单获取营销素材 - 性价比榜
 * (性价比榜是按照一些业务逻辑处理并经过运营人员调整过的，并不完全按真实性价比上榜)
 */
public class CostPerformanceRankHandler implements MarketingElementsHandler {

    private static final List<String> COST_PERFORMANCE_PRODUCTS = Arrays.asList(
            "1001", "1005", "1010", "1015", "1020"
    );

    @Override
    public MarketingElementsDTO handle(ProductDTO product, UserDTO user) {
        if (!COST_PERFORMANCE_PRODUCTS.contains(String.valueOf(product.getId()))) {
            return null;
        }

        //这里直接写死了一些测试数据，实际项目中是通过发其它应用的接口、查数据库、经过一些繁琐的业务逻辑处理来获取数据的
        MarketingElementsDTO elements = new MarketingElementsDTO();
        elements.setRecommendText("【性价比榜】" + product.getName() + "，入选平台性价比榜！");
        elements.setProductHighlights("平台性价比商品，性价比领先");
        elements.setMarketingImageUrl("https://example.com/marketing/rank/hot/" + product.getId() + ".jpg");
        elements.setExtraInfo("<a href='/rank/cost_performance'>查看性价比榜</a>");
        elements.setSource("cost_performance_rank");

        Random random = new Random();
        int randomTime = random.nextInt(15);
        //模拟实际业务中的耗时
        try {
            TimeUnit.MILLISECONDS.sleep(randomTime);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        return elements;
    }
}